﻿package br.gov.bcb.leilao.web.componentes.behavior;

import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.transformer.AbstractTransformerBehavior;

public class TransformarParaLabel extends AbstractTransformerBehavior {

    private static final String FECHAR_ATRIBUTO = "\" ";
    private static final String UNDERLINER = "_";

    @Override
    public CharSequence transform(Component component, CharSequence output) {
        if (component instanceof WebMarkupContainer) {
            return tranformarEmLabel(component);
        }
        return output;
    }

    private CharSequence tranformarEmLabel(Component component) {
        WebMarkupContainer webComponent = (WebMarkupContainer) component;

        return criarHtmlDoLabel(webComponent);
    }

    private String criarHtmlDoLabel(WebMarkupContainer webComponent) {
        String wicketId = webComponent.getMarkupId();
        String id = webComponent.getId();
        String conteudoDoLabel = conteudo(webComponent);

        return montarHtmlDoLabel(webComponent, wicketId, id, conteudoDoLabel);
    }

    private String conteudo(WebMarkupContainer webComponent) {
        return webComponent.getDefaultModelObjectAsString();
    }

    private String montarHtmlDoLabel(WebMarkupContainer componente, String wicketId, String id, String conteudoDoLabel) {
        StringBuilder sb = new StringBuilder();

        sb.append("<span ");
        adicionarAtributos(componente, wicketId, id, sb);
        sb.append(">");

        sb.append(conteudoDoLabel);

        sb.append("</span>");

        return sb.toString();
    }

    private void adicionarAtributos(WebMarkupContainer componente, String wicketId, String id, StringBuilder sb) {
        adicionarWicketId(wicketId, componente, sb);
        adicionarId(id, sb);
        adicionarWicketPath(componente, sb);
        adicionarNameSpace(sb);
    }

    private void adicionarWicketId(String wicketId, WebMarkupContainer componente, StringBuilder sb) {
        if (Application.DEVELOPMENT.equals(componente.getApplication().getConfigurationType())) {
            sb.append("wicket:id=\"");
            sb.append(wicketId);
            sb.append(FECHAR_ATRIBUTO);
        }
    }

    private void adicionarId(String wicketId, StringBuilder sb) {
        sb.append("id=\"");
        sb.append(wicketId);
        sb.append(FECHAR_ATRIBUTO);
    }

    private void adicionarWicketPath(WebMarkupContainer componente, StringBuilder sb) {
        if (componente.getApplication().getDebugSettings().isOutputComponentPath()) {
            sb.append("wicketpath=\"");
            sb.append(gerarWicketPath(componente.getPageRelativePath()));
            sb.append(FECHAR_ATRIBUTO);
        }

    }

    private String gerarWicketPath(String relativePath) {
        String wicketPath = relativePath.replace(UNDERLINER, "__");
        return wicketPath.replace(":", UNDERLINER);
    }

    private void adicionarNameSpace(StringBuilder sb) {
        sb.append("xmlns:wicket=\"http://wicket.apache.org\"");
    }

}
